const Base = require('./base');

class Mp3 extends Base {

    async getPlaylist() {
        const mp3list = await this.db.table('mp3 m').field(` *, ROUND( duration / 60 / 60 ) hour,
        ROUND( duration / 60) min,
        ROUND( duration mod  60) sec `)
        .where({ 
            download_status: ['=', 1],
        }) 
            .select();
        let result =  mp3list.map(m => {
           return {
            id: m.id,
            name: m.song_name,
            artistsname: m.author_name,
            duration_str:m.duration_str,
            picurl: "/musicMp3/01.jpg",
            url: `/musicMp3/${m.file_name}`,
            musicDuration:
                   m.duration_str ? m.duration_str :
                   `${m.hour ? m.hour + ':' : ''}${m.min <= 9 ? '0' + m.min : m.min}:${m.sec <= 9 ? '0' + m.sec : m.sec}`
           }
        })
        if (result) { 
            for (let item of result) {
                if (!item.duration_str) { 
                   await this.db.update({duration_str:item.musicDuration},{id:item.id})
                }
            }
        }
        return result;
    }

    async getWaitDownloadMp3() {
        const mp3list = await this.db.table('mp3 m')
            .where({
                keywords: ['is not', null],
                file_name: ['is', null],
                http_url: ['is', null],
                empty_search: ['=', -1],
                try_down_count: ['<=', 3],
                download_status: ['in',[-1,0]],
            }) 
            .limit(1).select();
        if (mp3list && mp3list.length == 1) {
            return mp3list[0]
         }
        return null;
    }
}

module.exports = Mp3;